package com.mouse.dao;

import com.mouse.po.User;
import com.mouse.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Auther: liu
 * @Date: 2024/5/23 15:12
 */
public class UserDao {
    public User login(String username, String pwd) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;

        try {
            assert JDBCUtil.getJdbcDemo() != null;

            connection = JDBCUtil.getJdbcDemo().getConnection();

            if (connection == null) {
                throw new Exception("Failed to establish a database connection.");
            }

            String sql = "SELECT id, user_name, pwd, real_name FROM user WHERE user_name = ? AND pwd = ?";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, username);
            preparedStatement.setString(2, pwd);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("user_name"));
                user.setPwd(resultSet.getString("pwd"));
                user.setRealname(resultSet.getString("real_name"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return user;
    }

    /**
     * user存入数据库的方法
     * @param user
     * @return
     */
    public void insertUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //User user = null;

        try {
            assert JDBCUtil.getJdbcDemo() != null;

            connection = JDBCUtil.getJdbcDemo().getConnection();

            if (connection == null) {
                throw new Exception("Failed to establish a database connection.");
            }

            String sql = " insert into user(id,user_name,pwd,real_name) value(?,?,?) ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPwd());
            preparedStatement.setString(3, user.getRealname());
            //执行sql
            preparedStatement.executeUpdate();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //return user;
    }

    /**
     * 根据username获取user信息，看是否已经存在
     * @param username
     * @return
     */
    public User getByUsername(String username) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;

        try {
            assert JDBCUtil.getJdbcDemo() != null;

            connection = JDBCUtil.getJdbcDemo().getConnection();

            if (connection == null) {
                throw new Exception("Failed to establish a database connection.");
            }

            String sql = "SELECT id, user_name, pwd, real_name FROM user WHERE user_name = ? ";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, username);

            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("user_name"));
                user.setPwd(resultSet.getString("pwd"));
                user.setRealname(resultSet.getString("real_name"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return user;
    }
}

